package com.itc.bi.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itc.bi.constant.KafkaConstant;
import com.itc.bi.constant.SystemConstant;
import com.itc.bi.dto.kafka.DataSetDTO;
import com.itc.bi.entity.DataSetColumnEntity;
import com.itc.bi.entity.DirectoryEntity;
import com.itc.bi.entity.TableColumnEntity;
import com.itc.bi.mapper.DirectoryMapper;
import com.itc.bi.service.DataSetColumnService;
import com.itc.bi.service.TableColumnService;
import com.itc.bi.vo.excelVo.HeardVO;
import com.itc.bi.vo.excelVo.SubClassVO;
import com.itc.common.core.utils.StringUtils;
import com.itc.common.log.annotation.Log;
import com.itc.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: itc-airport-cloud
 * @BelongsPackage: com.itc.bi.service.impl
 * @Author: hecaiy
 * @CreateTime: 2024-06-15  15:02
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class SyncImportDataBaseImpl {

    @Autowired
    private DirectoryMapper directoryMapper;
    @Autowired
    private DataSetColumnService dataSetColumnService;
    @Autowired
    private ClickHouseJDBCService clickHouseJDBCService;
    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Async("asyncServiceExecutor")
    @Transactional(rollbackFor = Exception.class)
    public void importDataBase(SubClassVO dataInfo, String tableName, String tableNameNote, Long dtId) {
        //表头数据操作
        List<HeardVO> heardVos = dataInfo.getHeardVos();
        List<DataSetColumnEntity> list = new ArrayList<>();
        //拼接数据库表创建sql,创建数据库表
        StringBuilder sql = new StringBuilder();
        sql.append("CREATE TABLE IF NOT EXISTS `" + tableName + "` ( "+ SystemConstant.DATABASEREDUNDANCYCOLUMN +" Int64,");
        int j = 1;
        String columnName = "";

        clickHouseJDBCService.insertDataByTableNameHashMap(tableName, dataInfo.getBodyVos());

        //构建数仓，数据同步，将数据库表字段信息入库
        for (int i = 0; i < heardVos.size(); i++) {
            HeardVO heardVO = heardVos.get(i);
            String characterType = heardVO.getCharacterType();
            sql.append("`" + heardVO.getKey() + "` " + clickHouseJDBCService.getColumnNameType(characterType) + " COMMENT '" + heardVO.getTitle() + "',");
            DataSetColumnEntity entity = new DataSetColumnEntity();
            entity.setId(IdWorker.getId());
            entity.setDataSetId(dtId);
            entity.setColumnName(heardVO.getKey());
            entity.setColumnComment(heardVO.getTitle());
            entity.setColumnType(heardVO.getCharacterType());
            entity.setColumnPosition(j);
            j++;
            list.add(entity);
        }
        dataSetColumnService.remove(Wrappers.<DataSetColumnEntity>lambdaQuery().eq(DataSetColumnEntity::getDataSetId,dtId));
        dataSetColumnService.saveBatch(list);
        if(ObjectUtil.isNotEmpty(list)){
//            sql.append("`"+ SystemConstant.DATABASEREDUNDANCYCOLUMN +"` Int64 ");
            sql.deleteCharAt((sql.lastIndexOf(",")));
            sql.append(") ENGINE=MergeTree order by "+SystemConstant.DATABASEREDUNDANCYCOLUMN+";");
            //创建数仓目标表
            clickHouseJDBCService.createTable(sql.toString());
            List<Map<String, Object>> bodyVos = dataInfo.getBodyVos();
            //数据同步
            clickHouseJDBCService.insertDataByTableNameHashMap(tableName, bodyVos);
        }
//        //拼接insert语句  插入数据
//        StringBuilder insertSql = new StringBuilder();
//        insertSql.append("INSERT INTO `" + tableName + "` ( ");
//        StringBuilder value = new StringBuilder();
//        int k = 0;
//        Boolean isTrue = false;
//        for (int i = 0; i < bodyVos.size(); i++) {
//            Map<String, Object> map = bodyVos.get(i);
//            for (Map.Entry<String, Object> entry : map.entrySet()) {
//                if (!isTrue) {
//                    insertSql.append("`" + entry.getKey() + "`,");
//                }
//                Object keyValue = entry.getValue();
//                if(ObjectUtil.isNotEmpty(keyValue)){
//                    value.append("'" + entry.getValue() + "',");
//                }else {
//                    value.append("'',");
//                }
//            }
//            if (!isTrue) {
//                insertSql.append("`auto_id`");
//            }
//            value.append(IdWorker.getId() + "),(");
//            isTrue = true;
//            k++;
//            //50条数据入库一次
//            if (k % 5000 == 0 ) {
//                value.deleteCharAt((value.lastIndexOf("(")));
//                value.deleteCharAt((value.lastIndexOf(",")));
//                //数据入库操作
//                String insertSqlStr = insertSql.toString() +" ) VALUES ( "+ value.toString();
//                clickHouseJDBCService.insertData(insertSqlStr);
//                value.setLength(0);
//                k = 0;
//            }
//        }
//        //最后一次入库操作,避免数据丢失
//        if (StringUtils.isNotEmpty(value.toString())) {
//            value.deleteCharAt((value.lastIndexOf("(")));
//            value.deleteCharAt((value.lastIndexOf(",")));
//            //数据入库操作
//            String insertSqlStr = insertSql.toString() + " ) VALUES ( "+ value.toString();
//            clickHouseJDBCService.insertData(insertSqlStr);
//            value.setLength(0);
//        }
    }


    /**
     * 数据入库
     *
     * @param dataInfo  数据集详情
     * @param tableName 数据表名称
     */
    @Async("asyncServiceExecutor")
//    @Transactional(rollbackFor = Exception.class)
    public void importDataBaseUpdate(SubClassVO dataInfo, String tableName, String oldTableName,List<DataSetColumnEntity> dataSetColumnEntities, DirectoryEntity directoryEntity) {
        //数据更新为全量更新
        clickHouseJDBCService.createTableByColumnList(tableName, dataSetColumnEntities, "");
        clickHouseJDBCService.insertDataByTableNameHashMap(tableName, dataInfo.getBodyVos());
        //切换表
        directoryEntity.setTableName(tableName);
        directoryMapper.updateById(directoryEntity);
        //通知数据集已经更新
        DataSetDTO dataSetDTO = new DataSetDTO();
        dataSetDTO.setDtId(directoryEntity.getDtId());
        kafkaTemplate.send(KafkaConstant.KAFKA_TOPIC_DATA_SET_PROCESS, JSON.toJSONString(dataSetDTO));
        //通知成功之后删除旧表
        clickHouseJDBCService.dropTable(oldTableName);
    }

    /**
     * 数据入库
     *
     * @param dataInfo  数据集详情
     * @param tableName 数据表名称
     */
    @Async("asyncServiceExecutor")
    //@Transactional(rollbackFor = Exception.class)
    public void importDataBase(SubClassVO dataInfo, String tableName,Long dtId) {
        //表头数据操作
        List<HeardVO> heardVos = dataInfo.getHeardVos();
        List<DataSetColumnEntity> list = new ArrayList<>();
        //构建数仓，数据同步，将数据库表字段信息入库
        for (int i = 0; i < heardVos.size(); i++) {
            HeardVO heardVO = heardVos.get(i);
            DataSetColumnEntity entity = new DataSetColumnEntity();
            entity.setId(IdWorker.getId());
            entity.setDataSetId(dtId);
            entity.setColumnName(heardVO.getKey());
            entity.setColumnComment(heardVO.getTitle());
            entity.setColumnType(clickHouseJDBCService.getColumnNameTypeTrs(heardVO.getCharacterType()));
            entity.setColumnPosition(i+1);
            list.add(entity);
        }
        dataSetColumnService.remove(Wrappers.<DataSetColumnEntity>lambdaQuery().eq(DataSetColumnEntity::getDataSetId,dtId));
        dataSetColumnService.saveBatch(list);
        clickHouseJDBCService.createTableByColumnList(tableName, list, "");
        clickHouseJDBCService.insertDataByTableNameHashMap(tableName, dataInfo.getBodyVos());
    }
}
